// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Threading;
using System.Threading.Tasks;

namespace SnapObjects.Data
{
    internal abstract class UpdateExecutor
    {
        protected void UpdateResult(
            ref UpdateResult updateResult,
            UpdateCommand updateCommand)
        {
            updateResult.InsertedCount += updateCommand.InsertedCount;
            updateResult.DeletedCount += updateCommand.DeletedCount;
            updateResult.ModifiedCount += updateCommand.ModifiedCount;
        }

        protected void ExecuteOtherUpdatable(
            DataContext dataContext,
            UpdatableObject updatableObject,
            ref UpdateResult updateResult)
        {
            var saveResult = updatableObject.MapperTrackable.SaveChanges(dataContext, updatableObject.Parms);

            updateResult.InsertedCount += saveResult.InsertedCount;
            updateResult.DeletedCount += saveResult.DeletedCount;
            updateResult.ModifiedCount += saveResult.ModifiedCount;
        }

        protected async Task<UpdateResult> ExecuteOtherUpdatableAsync(
            DataContext dataContext,
            UpdatableObject updatableObject,
            UpdateResult updateResult,
            CancellationToken cancellationToken)
        {
            var saveResult = await updatableObject.MapperTrackable.SaveChangesAsync(dataContext, updatableObject.Parms, cancellationToken);

            updateResult.InsertedCount += saveResult.InsertedCount;
            updateResult.DeletedCount += saveResult.DeletedCount;
            updateResult.ModifiedCount += saveResult.ModifiedCount;

            return (UpdateResult)saveResult;
        }

        protected bool ExecuteSaveAction(
            IUpdatableModel updatableModel,
            SaveActionBehavior saveAction,
            SaveContext saveContext)
        {
            if (!(updatableModel is ISaveAction))
            {
                return false;
            }

            var actionTarget = (ISaveAction)updatableModel;

            if (saveAction == SaveActionBehavior.BeforeSave &&
                    !actionTarget.HasBeforeAction)
            {
                return false;
            }

            if (saveAction == SaveActionBehavior.AfterSave &&
                    !actionTarget.HasAfterAction)
            {
                return false;
            }

            if (saveAction == SaveActionBehavior.BeforeSave)
            {
                actionTarget.BeforeSaveAction(saveContext);
            }
            else
            {
                actionTarget.AfterSaveAction(saveContext);
            }

            return saveContext.Cancelled;
        }

        protected bool ExecuteSaveAction(
            IUpdatableAction updatableAction,
            SaveContext saveContext)
        {
            if (updatableAction == null)
            {
                return false;
            }

            updatableAction.SaveAction(saveContext);

            return saveContext.Cancelled;
        }
    }
}
